/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.Message;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.entity.CommonContent;
import com.hboxs.asl.service.CommonContentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.List;

/**
 * Controller - 通用内容
 */
@Controller("adminCommonContentController")
@RequestMapping("/asl-admin/common_content")
public class CommonContentController extends BaseController {

    @Resource(name = "commonContentServiceImpl")
    private CommonContentService commonContentService;

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model) {
        return "/admin/commonContent/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(CommonContent commonContent, RedirectAttributes redirectAttributes) {
        if (!isValid(commonContent)) {
            return ERROR_VIEW;
        }
        commonContentService.save(commonContent);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        List<CommonContent> ccs = commonContentService.findAll();
        if(ccs==null || ccs.size()==0){
            CommonContent cc = new CommonContent();
            commonContentService.save(cc);
            model.addAttribute("commonContent", cc);
        }else{
            model.addAttribute("commonContent", ccs.get(0));
        }

        return "/admin/common_content/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(CommonContent commonContent, RedirectAttributes redirectAttributes) {
        if (!isValid(commonContent)) {
            return ERROR_VIEW;
        }
        commonContentService.update(commonContent, "");
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:edit.htm";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, ModelMap model) {
        model.addAttribute("page", commonContentService.findPage(pageable));
        return "/admin/commonContent/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message delete(Long[] ids) {
        commonContentService.delete(ids);
        return SUCCESS_MESSAGE;
    }

}
